(()=>{
    // $$('.bigList')[4].style.height="123px"
    $$('.navLink')[9].classList.add('bc')
})()


let addBtn=document.querySelectorAll('.addBtn')

//分页将返回数据添加到页面上
let paging=(now=0,size=6)=>{
    let tbody=document.querySelector('.tbody')
    let message=null
    axios({
        method:'get',
        url:'/albumc/queryByPage',
        params:{
            pageNow:now,
            pageSize:size
        },
    }).then(data=>{
        message=data.data.key.data.list
        // console.log(message);
        if(message!=null){
            tbody.innerHTML=``
            if(message.length==0){
                tbody.innerHTML+=`
                    <tr>
                    <td class="op"></td>
                    <td class="op"></td>
                    <td class="op"></td>
                    <td class="empty">没有内容</td>
                    </tr>
                `
            }
            for(let i=0,len=message.length;i<len;i++){
                let messagei=message[i]
                tbody.innerHTML+=`
                <tr>
                    <td><i class="iconfont icon-duoxuankuang select" data-choose="false" userId="${messagei.id}"></i></td>
                    <td>${messagei.name}</td>
                    <td>${messagei.context}</td>
                    <td></td>
                    <td>${messagei.time}</td>
                    <td>${messagei.classify==undefined?'':messagei.classify}</td>
                    <td>
                        <i class="iconfont icon-chakan01 c" title="查看" userId="${messagei.id}" onclick="viewUserMessage(${messagei.id})"></i>
                    </td>
                </tr>
                `
            }
            //单选框
            SelectAll()
        }
    }).catch(err=>{
        console.log(err);
    })
}



let pagess=(now=1,size=6)=>{
    return new Promise((reslove,reject)=>{
        axios({
            method:'get',
            url:'/albumc/queryByPage',
            params:{
                pageNow:now||1,
                pageSize:size||6
            },
        }).then(data=>{
            console.log(data);
            let length=data.data.key.data.total
            reslove(Math.ceil(length/size))
        }).catch(err=>{
            console.log(err);
        })
    })
}
pagess().then(data=>{
    PageSs(data)
})
//这里是调用的page.js里面的分页函数，实现省略号效果（可以在别的页面调用）
function PageSs(a=-1,key=1){

        let ans=a
        if(key>a)key=a
        if(key==0)key=1
        localStorage.setItem("key", key)
        Btn(ans,key,btnNumber)
        paging(key)
        for(let i=0,len=10;i<len;i++){
            page[i].addEventListener('click',function(){
                if(page[i].innerHTML!='...'){
                    key=page[i].innerHTML
                    Btn(ans,key,btnNumber)
                    paging(key)
                    localStorage.setItem("key", key)
                }
            })
        }
        
        //左
        left.addEventListener('click',function(){
            key--
            localStorage.setItem("key", key)
            Btn(ans,key,btnNumber)
            paging(key)
        })
        //右
        right.addEventListener('click',function(){
            key++
            localStorage.setItem("key", key)
            Btn(ans,key,btnNumber)
            paging(key)
        })

}



//单选框函数（可以在写的页面调用）
function SelectAll(){
    function hasClass(obj,cn){
        //判断obj中有没有cnclass
        //创建一个正则表达式
        var reg=new RegExp("\\b"+cn+"\\b");
        return reg.test(obj.className);
    }
    let select=document.querySelectorAll('.select')
    let selectClick=(i,event)=>{
            if(hasClass(select[i],'icon-duoxuankuang1')){
                event.target.setAttribute('data-choose','false')
                select[i].className = 'iconfont icon-duoxuankuang select'
            }else{
                for(let j=0,len=select.length;j<len;j++){
                    if(i==j){
                        event.target.setAttribute('data-choose','true')
                        continue;
                    }
                    select[j].setAttribute('data-choose','false')
                }
            }
            for(let j=0,len=select.length;j<len;j++){
                if(select[j].getAttribute('data-choose') == 'true'){
                    select[j].className = 'iconfont icon-duoxuankuang1 select'
                    select[j].parentNode.parentNode.style.background="#F2F2F2"
                }else{
                    select[j].className = 'iconfont icon-duoxuankuang select'
                    select[j].parentNode.parentNode.style.background="#FFFFFF"
                }
            }
    }
    for(let j=0,len=select.length;j<len;j++){
        select[j].onclick=(event)=>{
            selectClick(j,event)
        }
    }
}

//点击查看
function viewUserMessage(id){
    let modifyHeard=document.querySelector('.modifyHeard')
    modifyHeard.style.display='block'
    console.log(id);
    albumMessage(id)
}

//关闭弹窗
function close(){
    let cancleModify=document.querySelector('.cancleModify')
    let modifyHeard=document.querySelector('.modifyHeard')
    let describe=document.querySelector('.describe')
    let checkPicture=document.querySelector('.checkPicture')
    cancleModify.addEventListener('click',()=>{
        modifyHeard.style.display='none'
        describe.innerHTML=''
        checkPicture.innerHTML=''
    })
}
close()

/*  */
function albumMessage(id){
    let describe=document.querySelector('.describe')
    let checkPicture=document.querySelector('.checkPicture')
    axios({
        url:'/picture/forum',
        params:{
            postId:id
        }
    }).then(data=>{
        let message=data.data.key.data
        console.log(message);
        describe.innerHTML=`${message.context}`
        if(message.path==null||message.path==""){
            checkPicture.innerHTML+=`
            <div class="emptyS">没有内容</div>
            `
        }else{
            let length=message.path.split('::').length
            for(let i=0;i<length;i++){
                checkPicture.innerHTML+=`
                <div class="pictureVessles">
                    <div class="picture" style="background-image:url(${message.path.split('::')[i]});"></div>
                </div>
                `
            }
        }
        let btnT=document.querySelector('.btnT')
        btnT.onclick=function(){
            update(id,0)
        }
        let btnF=document.querySelector('.btnF')
        btnF.onclick=function(){
            update(id,1)
        }

    }).catch(err=>{
        console.log(err);
    })
}

//通过，不通过
function update(ids,result){
    axios({
        method: 'get',
        url: '/albumc/update',
        params: {
            ids:ids,
            result:result,
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }).then(data => {
        let message=data.data.key
        console.log(message.message);
        if(message.code==20000){
            tigs(message.message,true)
            pagess().then(data=>{
                PageSs(data,localStorage.getItem('key'))
            })
            let modifyHeard=document.querySelector('.modifyHeard')
            let describe=document.querySelector('.describe')
            let checkPicture=document.querySelector('.checkPicture')
            modifyHeard.style.display='none'
            describe.innerHTML=''
            checkPicture.innerHTML=''
        }else{
            tigs(message.message,false)
            pagess().then(data=>{
                PageSs(data,localStorage.getItem('key'))
            })
            let modifyHeard=document.querySelector('.modifyHeard')
            let describe=document.querySelector('.describe')
            let checkPicture=document.querySelector('.checkPicture')
            modifyHeard.style.display='none'
            describe.innerHTML=''
            checkPicture.innerHTML=''
        }
    }).catch(err => {
        console.log(err);
    })
}